package com.pentofab.payrollmanagement.dao.impl;

import java.sql.BatchUpdateException;
import java.util.ArrayList;
import java.util.Date;

import com.pentofab.payrollmanagement.dao.UserAccountsDao;
import com.pentofab.payrollmanagement.dao.model.UserAccounts;
import com.pentofab.payrollmanagement.database.UserAccountsRepository;
import com.pentofab.payrollmanagement.database.impl.UserAccountsRepositoryImpl;
import com.pentofab.payrollmanagement.exception.BusinessException;
import com.pentofab.payrollmanagement.exception.ExceptionMsg;
import com.pentofab.payrollmanagement.utilities.PayRollUtility;

public class UserAccountsDaoImpl implements UserAccountsDao {

	UserAccountsRepository userAccountsRepository;
	
	public UserAccountsDaoImpl(){
		
		userAccountsRepository =new UserAccountsRepositoryImpl();
	}
	
	@Override
	public void createUserAccount(UserAccounts accounts)
			throws BusinessException {

		if(accounts == null){
			return;
		}
		String validtidy = PayRollUtility.convertDateToString(PayRollUtility.convertStringToDate(accounts.getValidity()));
		accounts.setValidity(validtidy);
		if(PayRollUtility.compareDateWithToday(validtidy) < 1){
			throw new BusinessException(ExceptionMsg.INVALID_ACTIVE_TILL_DATE);
		}
		try {
		
			userAccountsRepository.createUserAccount(accounts);
		
		} catch (Exception e) {
			e.printStackTrace();
			throw new BusinessException(e,e.getMessage());
		}
		
	}

	@Override
	public void deleteUserAccount(String username) throws BusinessException {

		PayRollUtility.debug("Deleting user name :"+username);
		if(username == null){
			return;
		}
		
		try {
			
			userAccountsRepository.deleteUserAccount(username);
		} catch (Exception e) {
			e.printStackTrace();
			throw new BusinessException(e,e.getMessage());
		}
	}

	@Override
	public void modifyUserAccount(UserAccounts accounts, String oldUserName) throws BusinessException {

		System.out.println("accounts : "+accounts);
		try {
			if (accounts == null) {
				return;
			}
			String validtidy = PayRollUtility.convertDateToString(
					PayRollUtility.convertStringToDate(accounts.getValidity()));
			accounts.setValidity(validtidy);
			
			if (PayRollUtility.compareDateWithToday(validtidy) < 1) {
				
				throw new BusinessException(ExceptionMsg.INVALID_ACTIVE_TILL_DATE);
			}

			System.out.println("--------------- sathosh ----------------------------");
			userAccountsRepository.modifyUserAccount(accounts, oldUserName);

		} catch (Exception e) {
			e.printStackTrace();
			throw new BusinessException(e,e.getMessage());
		}
		
	
	
	}

	@Override
	public ArrayList<UserAccounts> getUserAccountsByParent(String parentUserName)
			throws BusinessException {
		
		if(parentUserName == null){
			return null;
		}
		
		 ArrayList<UserAccounts> accList  =new  ArrayList<UserAccounts>();
		 
		 try {
			 
			userAccountsRepository.getUserAccountsByParent(parentUserName, accList);
		} catch (Exception e) {
			throw new BusinessException(e,ExceptionMsg.USER_ACCOUNT_ERROR);
		
		}
		
		return (accList.size() > 0) ?  accList :null;
	}

	@Override
	public UserAccounts getUserAccountsByUserName(String userName)
			throws BusinessException {
		
		UserAccounts account = null;
		if(userName == null || userName.trim().equals("")){
			throw new BusinessException(ExceptionMsg.USER_NAME_NULL);
		}
		
		 try {
			 
			 account = userAccountsRepository.getUserAccountsByUserName(userName);
			 if(account == null){
				 throw new BusinessException(String.format(USER_NOT_FOUND, userName));
				 
			 }
			 
			 return account;
			}catch (BusinessException e) {
				
				throw e;
			} catch (Exception e) {
				
				throw new BusinessException(e,ExceptionMsg.USER_ACCOUNT_ERROR);
			}
		
		
	}

}
